package net.bac.sbe.web.report;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public abstract class ReportGenerator {


	public static final String ANALYST_NAME_KEY = "ANALYST";
	public static final String PROJECT_NAME_KEY = "PROJECT_NAME";
	public static final String PROJECT_ID_KEY = "PROJECT_ID";
	public static final String REPORT_DATA_SET_1 = "dataSet1";
	public static final String REPORT_DATA_SET_2 = "dataSet2";
	public static final String REPORT_FORMAT_KEY = "reportFormat";
	public static final String REPORT_ABSOLUTE_PATH_KEY = "reportAbsolutePath";
	public static final String NEW_ITEM_QUANTITY_KEY = "newItems";
	public static final String ITEM_QUANTITY_KEY = "dataRows";
	public static final String MODIFIED_ITEMS_QUANTITY_KEY = "modifiedItems";



	protected static final String ITEMS_TO_RETAIN_REPORT = "ItemsToRetainReport";
	protected static final String SONAR_FILTERED_REPORT = "SonarFilteredReport";




	public abstract void generateReport(HashMap<String, Object> reportParameters);

	protected String getReportTemplate(String reportTemplateId) {
		String templateReport = null;
		if(ITEMS_TO_RETAIN_REPORT.equals(reportTemplateId)){
			templateReport = "/net/bac/sbe/web/report/templates/ItemsToRetainReport.jasper";
		}
		if(SONAR_FILTERED_REPORT.equals(reportTemplateId)){
			templateReport = "/net/bac/sbe/web/report/templates/SonarFilteredReport.jasper";
		}
		return templateReport;
	}

	protected JRDataSource createDataSourceFromMatrix(
			final Map<String, List<Object>> data) {
		JRDataSource ds = new JRDataSource(){
			private int counter = -1;
			public Object getFieldValue(JRField arg0) throws JRException {
				return data.get(arg0.getName()).get(counter);
			}

			public boolean next() throws JRException {
				if(null == data || data.size() == 0){
					return false;
				}
				if(data.entrySet().iterator().next().getValue().size() - 1 > counter){
					counter++;
					return true;
				}
				return false;
			}

		};
		return ds;
	}

}
